package runner.ui;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: AVK
 * Date: 09.11.13
 * Time: 0:20
 * To change this template use File | Settings | File Templates.
 */
public class Renderer implements TreeCellRenderer {
    private final TreeItem theItem = new TreeItem();
    private final DefaultTreeCellRenderer defaultRenderer =
            new DefaultTreeCellRenderer();
    private final Color selectionForeground, selectionBackground;
    private final Color textForeground, textBackground;

    protected TreeItem getTheItem() {
        return theItem;
    }

    public Renderer() {
        final Font fontValue = UIManager.getFont("Tree.font");
        if (fontValue != null) theItem.label.setFont(fontValue);
        final Boolean focusPainted =
                (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
        theItem.checkBox.setFocusPainted(focusPainted != null && focusPainted);
        selectionForeground = UIManager.getColor("Tree.selectionForeground");
        selectionBackground = UIManager.getColor("Tree.selectionBackground");
        textForeground = UIManager.getColor("Tree.textForeground");
        textBackground = UIManager.getColor("Tree.textBackground");
    }

    @Override
    public Component getTreeCellRendererComponent(final JTree tree,
                                                  final Object value, final boolean selected, final boolean expanded,
                                                  final boolean leaf, final int row, final boolean hasFocus) {
        Data data = null;
        if (value instanceof DefaultMutableTreeNode) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            final Object userObject = node.getUserObject();
            if (userObject instanceof Data) {
                data = (Data) userObject;
            } else
                return defaultRenderer.getTreeCellRendererComponent(tree, value,
                        selected, expanded, leaf, row, hasFocus);
        }

        final String stringValue =
                tree.convertValueToText(value, selected, expanded, leaf, row, false);
        theItem.label.setText(stringValue);
        theItem.checkBox.setSelected(false);
        theItem.setEnabled(tree.isEnabled());
        if (selected) {
            theItem.setForeground(selectionForeground);
            theItem.setBackground(selectionBackground);
            theItem.checkBox.setBackground(selectionBackground);
            theItem.label.setForeground(selectionForeground);
            theItem.label.setBackground(selectionBackground);
        } else {
            theItem.setForeground(textForeground);
            theItem.setBackground(textBackground);
            theItem.checkBox.setBackground(textBackground);
            theItem.label.setForeground(textForeground);
            theItem.label.setBackground(textBackground);
        }
        theItem.label.setText(data.text);
        theItem.checkBox.setSelected(data.isSelected);
        return theItem;
    }
}
